package com.mobilepay.moneypig.model.activity.mine;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.response.mine.UserDetailData;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

/**
 * Created by xlm on 2017/7/24 0024
 */

public class BankCardActivity extends BaseActivity {
    private static final String TAG = "BankCardActivity";
    private RelativeLayout mRelativeBack;
    private RelativeLayout mTextAdd;
    private TextView mTextBankName, mTextBankNum;
    private ImageView mImageAvatar;
    private LinearLayout mLinearCard;
    private SmartRefreshLayout refreshLayout;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_bank_card);
    }

    @Override
    protected void initViews() {
        mRelativeBack = (RelativeLayout) findViewById(R.id.bank_back);
        mTextAdd = (RelativeLayout) findViewById(R.id.card_add);
        mTextBankName = (TextView) findViewById(R.id.bank_name);
        mTextBankNum = (TextView) findViewById(R.id.card_num);
        mImageAvatar = (ImageView) findViewById(R.id.bank_card_image);
        mLinearCard = (LinearLayout) findViewById(R.id.bank_card);
        refreshLayout = (SmartRefreshLayout) findViewById(R.id.bank_refresh);
        refreshLayout.setEnableLoadmore(false);
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        setData();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setData();
    }

    String bankNum;
    String bankName;

    private void setData() {
        bankNum = App.getApp().getUserDetailInfo().getData().getBank_card_code();
        bankName = App.getApp().getUserDetailInfo().getData().getBank_name();
        if (!CommonUtil.isEmpty(bankNum) && !CommonUtil.isEmpty(bankName)) {
            mTextBankName.setText(bankName);
            String newString = "";
            newString = CommonUtil.StringOpt(bankNum, 3, bankNum.length() - 4);
//            if (bankNum.length() > 16) {
//                newString = CommonUtil.StringOpt(bankNum, 3, bankNum.length()-4);
//            } else {
//                newString = CommonUtil.StringOpt(bankNum, 3, 12);
//            }
            mTextBankNum.setText(newString);
            //显示银行图片
            Glide.with(getThis()).load(CommonUtil.getPathFromAssets(bankName, getThis())).into(mImageAvatar);
            mLinearCard.setVisibility(View.VISIBLE);
        } else {
            mLinearCard.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onSetListener() {
        mRelativeBack.setOnClickListener(this);
        mTextAdd.setOnClickListener(this);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getUserDetail();
                refreshLayout.finishRefresh();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bank_back:
                finish();
                break;
            case R.id.card_add:
                startActivity(AddCardActivity.class, false, false);
                break;
        }
    }

    //账户信息
    private void getUserDetail() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.get_detail);
        param.add("uid", String.valueOf(App.getApp().getUserInfo().getData().getUid()));
        param.add("sign", param.getMd5String());
        showProgress("加载中");
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                dismissProgress();
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                dismissProgress();
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (responseResult.getStatus() == 0) {
                        showToastShort(responseResult.getInfo());
                    } else {
                        final UserDetailData data = GsonUtil.GsonToBean(string, UserDetailData.class);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                App.getApp().setUserDetailInfo(data);//保存账户信息
                                setData();
                            }
                        });
                    }
                }
            }
        });
    }
}
